package com.example.yiwei.crm.view.Class.listviewClass;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.entity.dockingEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/4/10.
 */

public class ParticipationListAdapter extends BaseAdapter {
    private Context context;
    public static List<dockingEntity> beans;
    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;
    private static HashMap<Integer, Boolean> notSelected;
    class ViewHolder {

        TextView tvName;
        TextView txt_department;
        TextView txt_job;
        CheckBox cb;
    }

    public ParticipationListAdapter(Context context, List<dockingEntity> beans) {
        // TODO Auto-generated constructor stub
        this.beans = beans;
        this.context = context;
        isSelected = new HashMap<Integer, Boolean>();
        notSelected=new HashMap<>();
        // 初始化数据
        initDate();
    }


    // 初始化isSelected的数据
    public static void initDate() {
        for (int i = 0; i < beans.size(); i++) {
            getIsSelected().put(i, false);
        }
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return beans.size();
    }
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return beans.get(position);
    }
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        // 页面
        final ParticipationListAdapter.ViewHolder holder;
        final dockingEntity bean = beans.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        if (convertView == null) {
            convertView = inflater.inflate(
                    R.layout.participation, null);
            holder = new ParticipationListAdapter.ViewHolder();
            holder.cb = (CheckBox) convertView.findViewById(R.id.participation_select_checkBox1);
            holder.tvName = (TextView) convertView
                    .findViewById(R.id.participation_select_txt_name);
            holder.txt_job = (TextView) convertView
                    .findViewById(R.id.participation_select_txt_job);
            convertView.setTag(holder);
        } else {
            // 取出holder
            holder = (ParticipationListAdapter.ViewHolder) convertView.getTag();
        }
        holder.tvName.setText( bean.getManager());
        if (bean.getManager_job()==1){
            holder.txt_job.setText("总经理");
        }else if (bean.getManager_job()==2){
            holder.txt_job.setText("总监");
        }else if (bean.getManager_job()==3){
            holder.txt_job.setText("副总监");
        }else if (bean.getManager_job()==4){
            holder.txt_job.setText("总监助理");
        }else if (bean.getManager_job()==10){
            holder.txt_job.setText("健教组长");
        }else if (bean.getManager_job()==11){
            holder.txt_job.setText("健教组员");
        }else if (bean.getManager_job()==12){
            holder.txt_job.setText("健教助理");
        }else if (bean.getManager_job()==13){
            holder.txt_job.setText("健教主任");
        }else if (bean.getManager_job()==20){
            holder.txt_job.setText("协作组长");
        }else if (bean.getManager_job()==21){
            holder.txt_job.setText("协作组员");
        }else if (bean.getManager_job()==30){
            holder.txt_job.setText("集团客服组长");
        }else if (bean.getManager_job()==31){
            holder.txt_job.setText("集团客服组员");
        }else if (bean.getManager_job()==32){
            holder.txt_job.setText("主编");
        }else if (bean.getManager_job()==33){
            holder.txt_job.setText("设计");
        }else if (bean.getManager_job()==34){
            holder.txt_job.setText("编辑");
        }else if (bean.getManager_job()==35){
            holder.txt_job.setText("网络组长");
        }else if (bean.getManager_job()==36){
            holder.txt_job.setText("网络组员");
        }else if (bean.getManager_job()==40){
            holder.txt_job.setText("院内导诊");
        }else if (bean.getManager_job()==50){
            holder.txt_job.setText("院内客服组长");
        }else if (bean.getManager_job()==51){
            holder.txt_job.setText("院内客服组员");
        }else if (bean.getManager_job()==52){
            holder.txt_job.setText("院内数据专员");
        }else if (bean.getManager_job()==53){
            holder.txt_job.setText("院内客服主任");
        }
        // 监听checkBox并根据原来的状态来设置新的状态
        holder.cb.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (isSelected.get(position)) {
                    isSelected.put(position, false);
                    setIsSelected(isSelected);
                } else {
                    isSelected.put(position, true);
                    setIsSelected(isSelected);
                }
            }
        });
        if (getIsSelected().get(position)!=null){

        }else {
            getIsSelected().put(position, false);
        }
        // 根据isSelected来设置checkbox的选中状况
        holder.cb.setChecked(getIsSelected().get(position));

        return convertView;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        ParticipationListAdapter.isSelected = isSelected;

    }
    public static List<String> getNames(){
        List<String> list  = new ArrayList<>();
        for (int i = 0;i<beans.size();i++){
            if (getIsSelected().get(i)!=null){

            }else {
                getIsSelected().put(i, false);
            }
            if(isSelected.get(i)){
                list.add(beans.get(i).getManager());
            }
        }
        return list;
    }
    public static List<Integer> getIDs(){
        List<Integer> list  = new ArrayList<>();
        for (int i = 0;i<beans.size();i++){
            if (getIsSelected().get(i)!=null){

            }else {
                getIsSelected().put(i, false);
            }
            if(isSelected.get(i)){
                list.add(beans.get(i).getManager_id());
            }
        }
        return list;
    }
    public static void setState(){
        for (int i = 0;i<beans.size();i++){
            if(isSelected.get(i)){
                isSelected.put(i, false);
            }
        }
    }
}
